--[[
1、根据      得到storage地址和压缩比列
2、连接tracker，得到storage_res ，根据storage_res将图片流下载下来
3、 用img相关方法根据得到的比例将下载下来的图压缩
4、用      增加头文件，ngx.say(gfile)
]]--

------------------------组件加载-----------------------------
local imgutils = require("utils") --包含
local utils                    = require "gcs.utils"
local err_utils                = require "gcs.upload.error_utils"
local multipart = require("resty.gcsdfs.multipart")
local config_table = ngx.shared.gcs_config 
local cjson = require ("cjson") 
local tracker = require("resty.gcsdfs.tracker")
local storage    = require "resty.gcsdfs.storage" 
local image = imgutils.image
local tk_host= config_table:get("p_tracker_host")
local tk_port= config_table:get("p_tracker_port")
local store_connect_timeout = config_table:get("p_store_connect_timeout")
local store_keep_timeout = config_table:get("p_store_keep_timeout")
local store_keep_count = config_table:get("p_store_keep_count")
--------------------------------------------------------------------
--------------------------相关函数------------------------------------
utils.gcs_log("error","time1 ")
function down_storage(storage_res,tb_value,gfile)
  local str_gfile
  local arg = tb_value
  local st = storage:new()
  st:set_timeout(arg.timeout)
  local ok, err = st:connect(storage_res)
  if not ok then
    return nil,err,1100                             --错误码待定义
  end
  str_gfile,err = st:download_file_to_buff1(gfile)
  if not str_gfile then
    return nil,err,1008
  end

  --关闭storage
  local ok, err = st:set_keepalive(arg.keep_timeout,arg.keep_count)
  return str_gfile
end

-------------------------------------------------------------------------
local time1=ngx.time()


local  url=ngx.var.request_uri
local i,j=string.find(url,"/(%w+)/")
local tax=string.sub(url,i+1,j-1) --得到存储组

local i,j = string.find(url,".jpg")
local gfile = string.sub(url,2,j)--得到gfile地址

---------------------------------------------------
-----------------从storage得到图像流----------------------------------
local tk=tracker:new()
tk:set_timeout(30000)
local tracker_hosts = {}
tracker_hosts.host = tk_host
tracker_hosts.port = tk_port
local ok, err = tk:connect(tracker_hosts)
if not ok then
    --fun.gcs_log("error","upload-->connect error:'" .. err)
    ngx.say(" There is tracker connection error,please reconnection")
    return
end
tk:set_v4(true)
local storage_res, err = tk:query_storage_store(tax)
local tb_value={
  timeout = store_connect_timeout,
  keep_timeout = store_keep_timeout,
  keep_count = store_keep_count
}
local gfile1,err = down_storage(storage_res,tb_value,gfile)
if not gfile1 then
    utils.gcs_log("error","storage connect  is fail " ..err)
end


-----------------------------------------------------------------
-----------------------------------------------------------------

local args = ngx.req.get_uri_args()

local img = image:new(gfile1)

if args.width and args.height then
    utils.gcs_log("error","res")
    img:adaptive_resize(tonumber(args.width),tonumber(args.height))
    local  res = img:get_blob()

    --ngx.req.set_header("Content-Type", "image/jpeg")
    ngx.say(res)
else
    utils.gcs_log("error","lack parameter")
    ngx.say(gfile1)
end

utils.gcs_log("error","time2 ")